package com.example.administrator.basedemos.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.administrator.basedemos.R;
import com.example.administrator.basedemos.activity.ExchangeActivity;
import com.example.administrator.basedemos.bean.MainItem;
import com.example.administrator.basedemos.utils.Constants;
import com.example.administrator.basedemos.utils.RecycleUtil;
import com.example.administrator.basedemos.view.SQLFlowLayout;
import com.example.net.utils.TmtUtils;
import com.hwangjr.rxbus.RxBus;

import java.util.ArrayList;
import java.util.List;

import static com.example.administrator.basedemos.view.SQLFlowLayout.START_FROM_LEFT;


/**
 * 创建者     ZCL
 * 创建时间   2018/3/12 16:45
 * 描述	      ${TODO}
 * <p>
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */

public class MainMultiItemAdapter extends BaseMultiItemQuickAdapter<MainItem, BaseViewHolder> {
    String TAG = "MainAdapter";

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public MainMultiItemAdapter(List<MainItem> data) {
        super(data);
        addItemType(Constants.search_flow, R.layout.flow_vg);
        addItemType(Constants.popular_currency, R.layout.template_recycleview_with_title_nob);
        addItemType(Constants.hot_exchange, R.layout.template_recycleview_with_title_nob);
    }

    @Override
    protected void convert(BaseViewHolder helper, MainItem item) {
        int type = item.getItemType();
        ArrayList<String> list = item.getList();
        switch (type) {
            case 0:
                helper.addOnClickListener(R.id.near_search_del);
                SQLFlowLayout flowViewGroup = helper.getView(R.id.flowlayout);
                flowViewGroup.setDefaultDisplayMode(START_FROM_LEFT);
                flowViewGroup.removeAllViews();
                TextView tv;
                boolean b = list.size() == 0;
                LinearLayout ly = helper.getView(R.id.near_search_ly);
                ViewGroup.LayoutParams params = ly.getLayoutParams();
                if (b)
                    params.height = 0;
                else
                    ly.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                for (int i = 0; i < list.size(); i++) {
                    tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.item_flow, flowViewGroup, false);
                    String text = item.getList().get(i);
                    tv.setText(text);
                    tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            TmtUtils.midToast(mContext, text);
                            RxBus.get().post("FLOW", text);
                        }
                    });
                    flowViewGroup.addView(tv);
                }
                break;
            case 1:
                helper.setText(R.id.recycleview_title_title, mContext.getString(R.string.热门币种));
                RecyclerView recyclerView = helper.getView(R.id.recycleview_title_recycle);
                recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
                PopularCurrencyAdapter adapterEx = new PopularCurrencyAdapter(R.layout.item_popu_currency, list);
                adapterEx.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        if (position < list.size())
                            RxBus.get().post("FLOW", list.get(position));
                    }
                });
                recyclerView.setAdapter(adapterEx);
                break;
            case 2:
                helper.setText(R.id.recycleview_title_title, mContext.getString(R.string.热门交易所));
                RecyclerView recyclerViewEx = helper.getView(R.id.recycleview_title_recycle);
                RecycleUtil.setLayoutMangerVertical(recyclerViewEx, mContext);
                HotExchangeAdapter adapter = new HotExchangeAdapter(R.layout.item_currency, list);
                adapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        if (position < list.size()) {
                            Bundle bundle = new Bundle();
                            bundle.putString("Title", list.get(position));
                            Intent intent = new Intent(mContext, ExchangeActivity.class);
                            intent.putExtras(bundle);
                            mContext.startActivity(intent);
                        }

                    }
                });
                recyclerViewEx.setAdapter(adapter);
                break;

            default:
                break;
        }
    }

}
